package sys

import (
	"server/data/db"
	"server/public"
	"sync"
)

/*
*  -*- coding:utf8 -*-
*
*  系统相关信息
*
*  @Author  : chengms
*  @WebSite : chengms.com
*  @Email   : chengms2012@163.com
*  @TIME    : 2020/8/27 16:09
*  @File    : sysInfo.go
 */

type SystemInfo struct {
	CurrentUserId int
	mux           sync.Mutex
}

var SysInfo SystemInfo

func InitSysInfo() {
	InitUserId()

}

func InitUserId() {
	SysInfo.CurrentUserId = 100000
	sql := "select current_user_id from sys_info"
	res, err := db.MysqlDb.Query(sql)
	if err != nil {
		public.Loggerf.Errorf("Query user id failed: %v", err)
	}

	for res.Next() {
		if err = res.Scan(&SysInfo.CurrentUserId); err != nil {
			public.Loggerf.Errorf("Query user id failed: %v", err)
		}
	}

	public.Loggerf.Infof("Current user id: %v", SysInfo.CurrentUserId)
}

func UpdateCurrentUserId() {
	SysInfo.mux.Lock()
	defer SysInfo.mux.Unlock()

	//TODO: 数据持久化操作
	sql := "update sys_info set current_user_id = ? "
	res, err := db.MysqlDb.Update(sql, SysInfo.CurrentUserId)
	if err != nil {
		public.Loggerf.Warnf("Update err: %v, %v", res, err)

	}
}
